package com.dongdongsongcan.app.adapter;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.dongdongsongcan.app.R;
import com.dongdongsongcan.app.common.DDConstant;
import com.dongdongsongcan.app.control.ShoppingCarControl;
import com.dongdongsongcan.app.db.bean.ShoppingCarDBBean;
import com.dongdongsongcan.app.fragment.ShopDetailFoodlistFragment;
import com.su.dolphin.adapter.DpBaseAdapter;
import com.su.dolphin.core.proxy.ImageLoader;

/**
 * Created by sfshine@qq.com on 2016/01/26 1014.
 */
public class ShopCarAdapter extends DpBaseAdapter<ShoppingCarDBBean> {
    private ShopDetailFoodlistFragment mShopDetailFoodlistFragment;

    public ShopCarAdapter(Context context, ShopDetailFoodlistFragment shopDetailFoodlistFragment) {
        super(context);
        mShopDetailFoodlistFragment = shopDetailFoodlistFragment;
    }

    @Override
    public int onGetItemLayout() {
        return R.layout.layout_food_item;
    }

    @Override
    public void onGetView(View convertView, int position) {
        final ShoppingCarDBBean shoppingCarDBBean = mDataList.get(position);
        TextView food_item_name =
                findViewById(convertView, R.id.food_item_name);
        TextView food_item_count = findViewById(convertView, R.id.food_item_count);
        TextView food_item_price = findViewById(convertView,
                R.id.food_item_price);

        food_item_price.setText(DDConstant.RMB + mDataList.get(position).getPrice());
        food_item_name.setText(mDataList.get(position).getProudctname());
        food_item_count.setText(mDataList.get(position).getCount() + "");
        ImageView food_item_icon = findViewById(convertView,
                R.id.food_item_icon);
        ImageLoader.i().loadImage(food_item_icon, shoppingCarDBBean.getImg());
        ImageView food_item_add = findViewById(convertView, R.id.food_item_add);
        ImageView food_item_delete = findViewById(convertView, R.id.food_item_delete);

        food_item_add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                shoppingCarDBBean.setId(0);
                ShoppingCarControl.i().save(shoppingCarDBBean);
                mShopDetailFoodlistFragment.reloadShopCar();
            }
        });
        food_item_delete.setOnClickListener(
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        ShoppingCarControl.i().delete(shoppingCarDBBean.getProductid());
                        mShopDetailFoodlistFragment.reloadShopCar();
                    }
                });
    }
}
